<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
			style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column prop="id" label="ID" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="订单标识" width="100" sortable>
			</el-table-column>
			<el-table-column prop="businessName" label="商机名称" width="180" sortable>
			</el-table-column>
			<el-table-column prop="productName" label="产品名称" width="120" sortable>
			</el-table-column>
			<el-table-column prop="price" label="价格" min-width="120" sortable>
			</el-table-column>
			<!--0未分配  1跟进中 2已转化为商机 -1放入线索池-->
			<el-table-column prop="state" label="状态" width="180" sortable>
				<template scope="scope">
					<span v-if="scope.row.state == '0'" style="color: blue">待付尾款</span>
					<span v-if="scope.row.state == '1'" style="color: orange">已完成</span>
					<span v-if="scope.row.state == '-1'" style="color: red">已取消</span>
				</template>
			</el-table-column>
			<el-table-column prop="customerName" label="客户名称" min-width="120" sortable>
			</el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button size="small" @click="pay(scope.$index, scope.row)">缴费</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length === 0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize"
				:total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或修改-->
		<el-dialog title="新增或修改" :visible.sync="orderFormVisible" :close-on-click-modal="false">
			<el-form :model="order" label-width="80px" :rules="orderFormRules" ref="orderForm">
				<el-form-item label="标识" prop="sn">
					<el-input v-model="order.sn" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="简介" prop="intro">
					<el-input type="textarea" v-model="order.intro"></el-input>
				</el-form-item>

				<el-form-item label="部门经理" prop="manager">
					<!-- value-key="id" 必须写 -->
					<el-select v-model="order.manager" clearable value-key="id" placeholder="请选择">
						<!--下拉选项
						  :key="" 唯一标识
						  :label 选择之后展示到选择框中的值
						  :value 选中之后绑定给模型层的值 
						-->
						<el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item">
							<span style="float: left">{{ item.username }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="orderFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="handleSave" :loading="orderLoading">提交</el-button>
			</div>
		</el-dialog>

		// 缴费
		<el-dialog title="缴费" :visible.sync="payFormVisible" :close-on-click-modal="false">
			<el-form :model="order" label-width="80px" :rules="payFormRules" ref="payForm">
				<el-form-item label="订单标识" prop="sn">
					<el-input v-model="order.sn" disabled="disabled" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="价格" prop="price">
					<el-input v-model="order.price" disabled="disabled" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="产品名" prop="productName">
					<el-input v-model="order.productName" disabled="disabled"></el-input>
				</el-form-item>
				<el-form-item label="客户名" prop="customerName">
					<el-input v-model="order.customerName" disabled="disabled"></el-input>
				</el-form-item>
				<el-form-item label="缴费模式" prop="payModel">
					<el-select v-model="order.payModel" clearable value-key="id" placeholder="请选择">
						<el-option v-for="m in payModels" :key="m.id" :label="m.title" :value="m">
							<span style="float: left">{{ m.title }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ m.value }}</span>
						</el-option>
					</el-select>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="payFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="handlePay" :loading="payLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
import util from '../../common/js/util'

import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
	data() {
		return {
			query: {
				currentPage: 1,
				pageSize: 10,
				keyword: ''
			},
			pageInfo: {
				total: 0,
				rows: []
			},
			listLoading: false,
			sels: [],//列表选中列

			orderFormVisible: false,// 编辑界面是否显示
			payFormVisible: false,// 缴费界面是否显示
			orderLoading: false,
			payLoading: false,
			orderFormRules: {
				name: [
					{ required: true, message: '请输入名称', trigger: 'blur' }
				]
			},
			//新增界面数据
			order: {
				id: null,
				sn: '',
				bussiness: {},
				product: {},
				price: 0,
				custom: {},
				payModel: {}
			},
			// 所有的员工，用以选择部门经理
			employees: [],
			// 部门树，用以选择上级部门
			orderTree: {},
			// 付款模式
			payModels: []
		}
	},
	methods: {
		//获取部门页面
		getOrders() {
			this.listLoading = true; //发起请求之前显示加载条
			//发起请求获取数据
			this.$http.post("/order", this.query)
				.then(result => {
					result = result.data; //{success,result:{}}
					if (result.success) {
						this.pageInfo = result.data;
						console.log(this.pageInfo);
					} else {
						this.$message({
							message: '获取分页数据失败！' + result.message,
							type: 'error'
						});
					}
					this.listLoading = false;
				})
				.catch(result => {
					this.$message({
						message: '系统错误！',
						type: 'error'
					});
				})
		},

		// 高级查询
		search() {
			// 回到第一页
			this.query.currentPage = 1;
			this.getOrders();
		},

		// 添加
		handleAdd: function () {
			this.orderFormVisible = true;
			this.order = {
				name: '',
				intro: '',
				manager: {},
				parent: {}
			};
			this.getEmployees();
		},
		// 删除
		handleDel: function (index, row) {
			this.$confirm('确认删除该记录吗?', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				let id = row.id;
				console.log(id);
				this.$http.delete("/order/" + id)
					.then((result) => {
						this.listLoading = false;
						if (result.data.success) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getOrders();
						} else {
							this.$message({
								message: '删除失败',
								type: 'error'
							});
						}

					})
					.catch(result => {
						this.$message({
							message: '系统异常',
							type: 'error'
						});
					});
			});
		},

		// 修改
		handleEdit: function (index, row) {
			this.orderFormVisible = true;
			this.order = Object.assign({}, row);
		},

		// 获取所有的员工
		getEmployees() {
			this.$http.get("/employee")
				.then(result => {
					result = result.data;
					if (result.success) {
						this.employees = result.data;
					} else {
						this.$message({
							message: '获取所有员工失败！' + result.message,
							type: 'error'
						});
					}
					this.listLoading = false;
				})
				.catch(result => {
					this.$message({
						message: '系统错误！',
						type: 'error'
					});
				})
		},

		// 处理新增或修改后的保存
		handleSave() {
			this.$refs.orderForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.orderLoading = true;
						let para = Object.assign({}, this.order);
						this.$http.put("/order", para)
							.then(result => {
								result = result.data;
								if (result.success) {
									this.$message({
										message: '保存成功',
										type: 'success'
									});
									//关闭对话框
									this.orderFormVisible = false;
									//重置表单
									this.$refs['orderForm'].resetFields();
									//重新加载数据
									this.getOrders();
								} else {
									this.$message({
										message: '保存失败',
										type: 'error'
									});
								}
								this.orderLoading = false;
							})
					})
				}
			})
		},

		// 批量删除选择id
		selsChange: function (sels) {
			this.sels = sels;
		},

		// 页码选择改变
		handleCurrentChange(val) {
			this.query.currentPage = val;
			this.getOrders();
		},
		//批量删除
		batchRemove: function () {
			let ids = this.sels.map(item => item.id);
			console.log(ids);
			this.$confirm('确认删除选中记录吗？', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				this.$http.patch("/order", ids)
					.then((result) => {
						this.listLoading = false;
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getOrders();
					})
					.catch(result => {
						this.listLoading = false;
						this.$message({
							message: '删除失败',
							type: 'error'
						});
					});
			})
		},

		// 点击缴费按钮触发
		pay(index, row) {
			this.payFormVisible = true;
			this.getPayModels();
			this.order = Object.assign({}, row); //回显
		},

		handlePay() {
			this.payLoading = true;
			this.$http.put("/order/pay")
			.then(result => {
				result = result.data;
			})
			.catch(result => {

			})

			this.payLoading = false;
			this.payFormVisible = false;
		},

		getPayModels() {
			this.$http.get("/dictionaryItem/payModels")
				.then(result => {
					result = result.data;
					if (result.success) {
						this.payModels = result.data;
					}
				})
				.catch(result => {
					result = result.data;
					this.$message({
						message: '删除失败' + result.message,
						type: 'error'
					});
				});
		}

	},

	mounted() {
		this.getOrders();
	}
}

</script>

<style scoped>
</style>